package com.acwer.oa.wf;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collections;

import org.activiti.bpmn.model.BpmnModel;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.image.ProcessDiagramGenerator;
import org.activiti.image.impl.DefaultProcessDiagramGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class FlowUtils {
	
	protected String activityFontName = "宋体";
    protected String labelFontName = "宋体";
    protected String annotationFontName = "宋体";
    
	 /**
     * Get Process instance diagram
     */
	@Autowired
	RuntimeService runtimeService;
	
	@Autowired
	RepositoryService repositoryService;
	
	@Autowired
	ProcessEngine engine;	
	 
    public InputStream getProcessDiagram(String processInstanceIdOrDefinitionId) {
    	
        ProcessInstance processInstance = null;
        try {
        	processInstance=runtimeService.createProcessInstanceQuery().
    		processInstanceId(processInstanceIdOrDefinitionId).singleResult();
        	System.out.println("instance");
		} catch (Exception e) {
			// TODO: handle exception
		}
        
        
        ProcessDefinition definition=null;
        try {
        	definition=repositoryService.createProcessDefinitionQuery().
 			processDefinitionId(processInstanceIdOrDefinitionId).singleResult();
		} catch (Exception e) {
			// TODO: handle exception
		}
        
        String processDefinitionId=processInstance==null?definition.getId():processInstance.getProcessDefinitionId();
        
        
     // get process model
        BpmnModel model = repositoryService.getBpmnModel(processDefinitionId);

        if (model != null && model.getLocationMap().size() > 0) {
            ProcessDiagramGenerator generator = new DefaultProcessDiagramGenerator();
            
            // 生成流程图 已启动的task 高亮
            if(processInstance!=null){
              return generator.generateDiagram(model,
              runtimeService.getActiveActivityIds(processInstanceIdOrDefinitionId),Collections.emptyList(), activityFontName,
               labelFontName,
               annotationFontName,
              false,"na.png");
              
              
            }else{
            	// 生成流程图 都不高亮
                return generator.generateDiagram(model,activityFontName,labelFontName,annotationFontName);
            }
            
//            InputStream generateDiagram(BpmnModel bpmnModel,
//                    List<String> highLightedActivities,
//                    List<String> highLightedFlows,
//                    String activityFontName,
//                    String labelFontName,
//                    String annotationFontName,
//                    boolean generateDefaultDiagram,
//                    String defaultDiagramImageFileName);

            
        }
        return null;
    }
}
